package com.qijian.maindata.controller;

import java.util.List;

import com.qijian.maindata.domain.ProductAttributeItem;
import com.qijian.maindata.domain.query.ProductAttributeItemQuery;
import com.qijian.maindata.service.IProductAttributeItemService;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 辅助属性值Controller
 *
 * @author qijian
 * @date 2022-01-13
 */
@Api(tags = "辅助属性值")
@RestController
@RequiredArgsConstructor
@RequestMapping("/business/productAttributeItem")
public class ProductAttributeItemController extends BaseController {

    private final IProductAttributeItemService productAttributeItemService;

    /**
     * 查询辅助属性值列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductAttributeItemQuery query, IPage<ProductAttributeItem> page) {
        return getDataTable(productAttributeItemService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    @PreAuthorize("@ss.hasAnyPermi('maindata:item:query,business:eqp:list,business:repairTicket:list')")
    @GetMapping("/val/{itemId}")
    public TableDataInfo listVal(@PathVariable("itemId") Long itemId) {
        return getDataTable(productAttributeItemService.pageWithValByItemId(itemId));
    }

    /**
     * 导出辅助属性值列表
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:export')")
    @Log(title = "辅助属性值", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(ProductAttributeItemQuery query, HttpServletResponse response) {
        new ExcelUtil<>(ProductAttributeItem.class).exportExcel(response, productAttributeItemService.list(WhereEntityTool.invoke(query)), "辅助属性值数据");
    }

    /**
     * 获取辅助属性值详细信息
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId) {
        if (itemId == null)
            return AjaxResult.success();
        return AjaxResult.success(productAttributeItemService.getByItemId(itemId));
    }

    /**
     * 新增辅助属性值
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:add')")
    @Log(title = "辅助属性值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductAttributeItem productAttributeItem) {
        return toAjax(productAttributeItemService.save(productAttributeItem));
    }

//    @PreAuthorize("@ss.hasPermi('business:productAttributeItem:add')")
//    @Log(title = "辅助属性值", businessType = BusinessType.INSERT)
//    @PostMapping("saveUpdate")
//    public AjaxResult saveUpdate(@RequestBody List<ProductAttributeItem> productAttributeItem) {
//        productAttributeItemService.saveUpdate(productAttributeItem);
//        return AjaxResult.success();
//    }

    /**
     * 修改辅助属性值
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:edit')")
    @Log(title = "辅助属性值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductAttributeItem productAttributeItem) {
        return toAjax(productAttributeItemService.updateById(productAttributeItem));
    }

    /**
     * 删除辅助属性值
     */
    @PreAuthorize("@ss.hasPermi('maindata:item:remove')")
    @Log(title = "辅助属性值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(productAttributeItemService.removeByIds(ids));
    }
}
